<template>
  <div class="wrapper">
    <el-form ref="editAuth" :model="editData" :rules="rules" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input class="w500" v-model="editData.name" placeholder="例如：注册"></el-input>
      </el-form-item>
      <el-form-item label="应用名称" prop="app_name">
        <el-input class="w500" v-model="editData.app_name" placeholder="例如：yayiadmin"></el-input>
      </el-form-item>
      <el-form-item label="server" prop="server">
        <el-input class="w500" v-model="editData.server" placeholder="例如：serveruser"></el-input>
      </el-form-item>
      <el-form-item label="module" prop="module">
        <el-input class="w500" v-model="editData.module" placeholder="例如：user"></el-input>
      </el-form-item>
      <el-form-item label="controller" prop="controller">
        <el-input class="w500" v-model="editData.controller" placeholder="例如：User"></el-input>
      </el-form-item>
      <el-form-item label="action" prop="action">
        <el-input class="w500" v-model="editData.action" placeholder="例如：register"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="editAuth('editAuth')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import notify from '@/tools/notify'

export default {
  data () {
    return {
      rules: {
        name: [
          {required: true, message: '请输入权限名称', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
        server: [
          {required: true, message: '请输入server', trigger: 'blur'},
          {min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur'}
        ],
        module: [
          {required: true, message: '请输入module', trigger: 'blur'},
          {min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur'}
        ],
        controller: [
          {required: true, message: '请输入controller', trigger: 'blur'},
          {min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur'}
        ],
        action: [
          {required: true, message: '请输入action', trigger: 'blur'},
          {min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  computed: {
    // 获取该条编辑权限信息
    ...mapState({
      editData: state => state.auth.detail
    }),
    id () {
      return this.$route.params.id
    }
  },
  mounted () {
    this.getAuthDetail()
  },
  methods: {
    // 获取权限详情
    async getAuthDetail () {
      let payload = {
        id: this.id
      }
      await this.$store.dispatch('auth/authInfo', payload)
    },
    // 后台编辑权限 使用element中的form表单
    editAuth (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch('auth/editAuth', this.editData).then(res => {
            notify.success(res.msg)
            this.$router.push('/auth/list')
          }).catch(() => {})
        } else {
          return false
        }
      })
    },
    goBack () {
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped lang="scss">
@import "../../../assets/scss/base.scss";
.wrapper{
  .w500 {
    width: 500px;
  }
  .w100 {
    width: 100px;
  }
}
</style>